﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UnfriendlyFire.Models;
using System.ComponentModel.Composition;

namespace UnfriendlyFire.Controllers
{
    [Export(typeof(IController))]
    [ExportMetadata("Name", "Account")]
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
    public class AccountController : Controller
    {
        private IAccountServices _accountServices;
        private GameRepository _gameRepository;

        [ImportingConstructor]
        public AccountController(IAccountServices accountServices, GameRepository gameRepository)
        {
            _accountServices = accountServices;
            _gameRepository = gameRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SignUp()
        {
            return View(new NewUser());
        }

        [HttpPost]
        public ActionResult SignUp(NewUser newUser, string redirectUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(newUser);
            }
            try
            {
                _accountServices.SignUp(newUser.Email, newUser.Password, newUser.PasswordQuestion, newUser.PasswordAnswer, newUser.StaySignedIn);
                var officer = new Officer();
                officer.Email = newUser.Email;
                officer.FirstName = string.Empty;
                officer.LastName = string.Empty;
                officer.Photo = string.Empty;
                officer.ExperiencePoints = 0;
                officer.Rank = _gameRepository.Ranks.OrderBy(r => r.RequireExperiencePoints).First();
                _gameRepository.Officers.AddObject(officer);
                _gameRepository.SaveChanges();
                if (string.IsNullOrEmpty(redirectUrl))
                {
                    return RedirectToAction("Edit", "Setup");
                }
                return Redirect(redirectUrl);
            }
            catch (SignUpException e)
            {
                ModelState.AddModelError(e.Key, e.Message);
                return View(new NewUser());
            }
        }

        public ActionResult SignOut()
        {
            _accountServices.SignOut();
            return RedirectToAction("Index", "Home");
        }

        public ActionResult SignIn(string returnUrl)
        {
            return View(new ExistingUser() { StaySignedIn = true });
        }

        [HttpPost]
        public ActionResult SignIn(ExistingUser existingUser, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                ViewData["ReturnUrl"] = returnUrl;
                return View(existingUser);
            }
            try
            {
                _accountServices.SignIn(existingUser.Email, existingUser.Password, existingUser.StaySignedIn);
                if (!string.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                return RedirectToAction("Index", "Home");
            }
            catch (SignInException e)
            {
                ModelState.AddModelError(e.Key, e.Message);
                ViewData["ReturnUrl"] = returnUrl;
                return View(existingUser);
            }
        }
    }
}
